import {
  getDataByKey,
  saveDataByKey
} from '@/utils/util'
import {
  getTaskList,
  getExamineTaskDeatil,
  getExaminePointContentList
} from '@/api/inspect'
import store from '@/store/store'

export const getCommitWorkData = () => {
  let commitData = getDataByKey("offlineData-work-commit-data");
  if (!commitData) {
    commitData = "[]";
  }
  return JSON.parse(commitData);
}

export const updateInspectionData = (data) => {
  // 修改列表数据
  var param = {
    account: store.state.userInfo.accountName,
    status: 1, //执行中
  };
  let pointRes = JSON.parse(data.pointContentResultsSting)
  getTaskList(param).then(res => {
    let dataList = res.data
    if (dataList && dataList.length > 0) {
      for (var i = 0; i < dataList.length; i++) {
        if (dataList[i].id == data.taskId) {

          dataList[i].examined += 1
          dataList[i].unIsOkNum += (pointRes.filter(m => {
            return m.result == 2
          }).length == 0 ? 0 : 1)

          // 修改列表数据
          let key1 = "offlineData-" + "app/examine/getTaskList" + JSON.stringify(param);
          var dataJSON1 = {
            status: 200,
            info: "操作成功",
            total: dataList.length,
            data: dataList
          }
          saveDataByKey(key1, JSON.stringify(dataJSON1));

          // 修改任务详情
          let param3 = {
            taskId: data.taskId,
          }
          getExamineTaskDeatil(param3).then(res => {
            let dataJSON3 = {
              status: 200,
              info: "操作成功",
              data: res.data
            }
            dataJSON3.data.pointList.forEach(n => {
              if (n.id == data.pointId) {
                n.result = pointRes.filter(m => {
                  return m.result == 2
                }).length == 0 ? 1 : 2
              }
            })
            dataJSON3.data.examined += 1
            let key3 = "offlineData-" + "app/examine/getExamineTaskDeatil" + JSON.stringify(param3);
            saveDataByKey(key3, JSON.stringify(dataJSON3));
          })

          // 修改任务点位巡检结果
          getExaminePointContentList({
            pointCode: data.pointCode
          }).then(res => {
            let param4 = {
              taskId: data.taskId,
              pointId: data.pointId
            }
            let dataJSON4 = {
              status: 200,
              info: "操作成功",
              data: res.data
            }
            dataJSON4.data.contentList.forEach(n => {
              let item = pointRes.filter(m => {
                return m.contentId == n.id
              })[0]
              if (item) {
                n.result = item.result
                n.contentResult = item.contentResult
              }
            })
            let key4 = "offlineData-" + "app/examine/getTaskPointResult" + JSON.stringify(param4);
            saveDataByKey(key4, JSON.stringify(dataJSON4));
          })

          // 修改点位二维码扫描结果
          let param2 = {
            taskId: data.taskId,
            account: store.state.userInfo.accountName,
            pointCode: data.pointCode,
          };
          let key2 = "offlineData-" + "app/examine/getPointNowStatus" + JSON.stringify(param2);
          var dataJSON2 = {
            status: 200,
            info: "操作成功",
            total: 0,
            data: {
              info: "该点位已完成巡检",
              status: "205"
            }
          }
          saveDataByKey(key2, JSON.stringify(dataJSON2));
        }
      }
    }
  })

  console.log("存储提交的巡检数据");
  let commitDataJSON = getCommitWorkData();
  commitDataJSON.push({
    api: 'app/examine/commitPointResult',
    param: JSON.stringify(data),
    requestType: "post",
    userName: store.state.userInfo.accountName
  });
  saveDataByKey("offlineData-work-commit-data", JSON.stringify(commitDataJSON));
}

export const getAppData = () => {
  store.commit('setUserInfo', JSON.parse(getDataByKey('LOCAL_USERINFO')))
  store.commit('setQiniuPath', getDataByKey('QINIUPATH'))
  store.commit('setPlatformCode', getDataByKey('platformCode'))
  store.commit('setIsOffline', getDataByKey('IS_OFFLINE_MODULE') == 1 ? true : false)
  store.commit('setAppKeyObj', JSON.parse(getDataByKey('appKeyObj')))
  store.commit('setUnitRecords', JSON.parse(getDataByKey('unit_search_records') || '{}'))
}
